package org.mendoza.salud.hvt.ui.cruds;

import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import org.mendoza.salud.hvt.model.AbstractEntity;

/**
 * Created by administrador on 27/03/15.
 */

public class CrudGenerator<T extends AbstractEntity> extends CustomComponent implements View {
    T t;
    FormLayout layout = new FormLayout();
    String campos[];

    public CrudGenerator() {

    }

    public CrudGenerator(T bean, String... campos) {
        this.t = bean;
        this.campos = campos;
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent) {
        init();
    }

    private void init() {
        this.setCompositionRoot(layout);

        final BeanFieldGroup<T> binder =
                new BeanFieldGroup<T>((Class<T>) t.getClass());

        for (Object objeto : binder.getBoundPropertyIds()) {
            layout.addComponent(new Label("" + objeto));
        }

        layout.addComponent(new Label("Terminamos..."));
    }

}
